package com.bachongLightNovel.system.controller;


import com.bachongLightNovel.model.dto.system.AssignPermissionDto;
import com.bachongLightNovel.model.pojo.system.Role;
import com.bachongLightNovel.result.Result;
import com.bachongLightNovel.system.service.SystemRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/system/role")
@Slf4j
public class SystemRoleController {


    @Autowired
    private SystemRoleService systemRoleService;

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/list")
    public Result getRoleList(){
        log.info("获取角色列表");
        List<Role> list=systemRoleService.getRoleList();
        return Result.success(list);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @PostMapping("/save")
    public Result saveRole(@RequestBody Role role){
        log.info("新增角色：{}",role);
        systemRoleService.saveRole(role);
        return Result.success();
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @PutMapping("/update")
    public Result updateRole(@RequestBody Role role){
        log.info("编辑角色：{}",role);
        systemRoleService.updateRole(role);
        return Result.success();
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/removeById/{id}")
    public Result removeRoleById(@PathVariable Long id){
        log.info("根据id删除角色：{}",id);
        systemRoleService.removeRoleById(id);
        return Result.success();
    }

    /**
     * 根据用户id获取角色列表
     * @param userId
     * @return
     */
    @GetMapping("/getRoleByUserId/{userId}")
    public Result getRoleByUserId(@PathVariable Long userId){
        log.info("根据用户id获取角色列表：{}",userId);
        List<Long> voList=systemRoleService.getRoleByUserId(userId);
        return Result.success(voList);
    }

    /**
     * 为角色分配权限
     * @param dto:
     * @return Result
     * @author vermouth
     */
    @PostMapping("/assign")
    public Result assignPermission(@RequestBody AssignPermissionDto dto){
        log.info("为角色分配权限：{}",dto);
        systemRoleService.assignPermission(dto);
        return Result.success();
    }
}
